package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.MyBorrowService;
import com.woniu.p2p.entity.User;
import com.woniu.p2p.entity.UserLoans;
import com.woniu.p2p.listener.InBorrowListener;
import com.woniu.p2p.vo.BorrowVo;
import com.woniu.p2p.vo.UserAndBorrowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/myborrow")
public class MyBorrowController {

    @Autowired
    MyBorrowService borrowService;

    @RequestMapping("/getUserBorrow")
    public String getUserBorrow(Map map, String borrowstatus,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") Date predate,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") Date nextdate,
                               String borrowkeyword,
                               Integer pagenum,
                               Integer pagesize){
        pagenum=pagenum==null?1:pagenum;
        pagesize=pagesize==null?3:pagesize;
        PageInfo<BorrowVo> pageInfo = borrowService.findAllBorrow(pagenum, pagesize, borrowstatus, predate, nextdate, borrowkeyword);
        map.put("userBorrow",pageInfo);
        map.put("borrowstatus",borrowstatus);
        map.put("predate",predate);
        map.put("nextdate",nextdate);
        map.put("borrowkeyword",borrowkeyword);
        map.put("pagesize",pagesize);
        return "content/borrow";
    }


    @RequestMapping("/getborrowdetail")
    @ResponseBody
    public UserAndBorrowVo getdetail(Integer id){
        UserLoans userLoans = borrowService.findAllUserLoansByLoansId(id);
        Integer userid = userLoans.getUserid();
        User user = borrowService.findAllUserById(userid);
        String userHead = user.getUserHead();
        Long userAmount = user.getUserAmount();
        String userRealname = user.getUserRealname();
        Integer userCode = user.getUserCode();
        Long userGetmoney = user.getUserGetmoney();
        Date loansPaytime = userLoans.getUserPaytime();
        String loansTime = userLoans.getUserTime();
        Date loansDealtime = userLoans.getUserDealtime();
        Long loansAmount = userLoans.getUserAmount();
        Long userBalance = userLoans.getUserBalance();
        String loansStatus = userLoans.getUserStatus();
        UserAndBorrowVo userAndBorrowVo = new UserAndBorrowVo( userHead, userAmount, userRealname, userCode, userGetmoney, loansPaytime, loansTime, loansDealtime, loansAmount, loansStatus, userBalance);
        System.out.println(userAndBorrowVo);
        return userAndBorrowVo;
    }


    @RequestMapping("/deloneborrow")
    public int deloneborrow(int id){
        borrowService.deleteBorrowById(id);
        return 1;
    }

    @RequestMapping("/delallborrow")
    public int delallborrow(int[] id){
        for (int i = 0; i <id.length ; i++) {
            borrowService.deleteBorrowById(id[i]);
        }
        return 1;
    }

    /**
     * 导出
     */
    @RequestMapping("/outputborrow")
    public void outputborrow(int[] ids,
                            HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");


        List<BorrowVo> list = borrowService.findAllBorrow(ids);
        ExcelWriterBuilder write = EasyExcel.write(outputStream, BorrowVo.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(list);
        outputStream.close();
    }

    @Autowired
    InBorrowListener inRepayListener;


    /**
     * 导入
     */
    @RequestMapping("/inborrow")
    @ResponseBody
    public Integer inborrow(@RequestParam("file") MultipartFile file){
        try {
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), BorrowVo.class,inRepayListener);
            ExcelReaderSheetBuilder sheet = read.sheet();
            sheet.doRead();

        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }



}
